// pages/content/content.js
var netWork = require('../../assets/js/network.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultValue:'naruto',
    hotSear:[],
    historys:['你好','不好','好个屁'],
    historyState:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.powerRationing();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if(wx.getStorageSync('hotWord'))
      this.setData({ historys: wx.getStorageSync('hotWord'), historyState:true})
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  powerRationing:function(){
    var This = this;
    wx.request({
      url: `${netWork.api}wzhotkey/list?pageSize=20&currentPage=1&sidx=searchque&order=desc`,
      success:function(res){
        if(!res.data.code)
          This.setData({
            hotSear:res.data.page.list
          })
      }
    })
  },
  clearValue:function(){
    let This = this;
    wx.showModal({
      content:'确定清除搜索历史吗？',
      mask:true,
      success:function(e){
        console.log('点击',e)
        if(e.confirm){
          wx.removeStorageSync('hotWord');
          This.setData({
            historys: [],
            historyState: false
          })
        }else if(e.cancel){
          
        }
      }
    })
    
    
  },
  searValue:function(event,e){

    if (wx.getStorageSync('hotWord')){
      let init = wx.getStorageSync('hotWord');
      init.push(event.detail.value)
      wx.setStorageSync('hotWord', init);
    } else{
      let init = []
      init.push(event.detail.value)
      wx.setStorageSync('hotWord',init)
    }
    this.setData({
      historys: wx.getStorageSync('hotWord'),
      historyState:true
    })
    wx.navigateTo({
      url: `../../pages/searResponse/response?text=${event.detail.value}`,
    })
  },
  searStart:function(e){
    wx.navigateTo({
      url: `../../pages/searResponse/response?text=${e.currentTarget.dataset.name}`,
    })
  }
})